﻿Imports System.Runtime.InteropServices

''' <summary>
''' 屏障全角
''' </summary>
''' <remarks>
''' Create Person:huyag
''' Create Date:2008/05/02
''' Bug:當變為全時,要先最小化，然後最大化才能把全角轉變為半角
''' 不知道怎麼樣可以直接轉換不用最小化程序
''' Remark:C#版轉VB.NET
''' </remarks>
Public Class Form_ImeMode

    ''[DllImport("imm32.dll")]
    ''public static extern IntPtr ImmGetContext(IntPtr hwnd);
    Declare Auto Function ImmGetContext Lib "imm32.dll" Alias "ImmGetContext" (ByVal hwnd As System.IntPtr) As System.IntPtr

    ''[DllImport("imm32.dll")]
    ''public static extern bool ImmGetOpenStatus(IntPtr himc);
    Declare Auto Function ImmGetOpenStatus Lib "imm32.dll" Alias "ImmGetOpenStatus" (ByVal himc As System.IntPtr) As Boolean

    ''[DllImport("imm32.dll")]
    ''public static extern bool ImmSetOpenStatus(IntPtr himc, bool b);
    Declare Auto Function ImmSetOpenStatus Lib "imm32.dll" Alias "ImmSetOpenStatus" (ByVal himc As System.IntPtr, ByVal b As Boolean) As Boolean

    ''[DllImport("imm32.dll")]
    ''public static extern bool ImmGetConversionStatus(IntPtr himc, ref   int lpdw, ref   int lpdw2);
    Declare Auto Function ImmGetConversionStatus Lib "imm32.dll" Alias "ImmGetConversionStatus" (ByVal himc As System.IntPtr, ByRef lpdw As Integer, ByRef lpdw2 As Integer) As Boolean

    ''[DllImport("imm32.dll")]
    '' public static extern int ImmSimulateHotKey(IntPtr hwnd, int lngHotkey);
    Declare Auto Function ImmSimulateHotKey Lib "imm32.dll" Alias "ImmSimulateHotKey" (ByVal hwnd As System.IntPtr, ByVal lngHotkey As Integer) As Integer

    '' private const int IME_CMODE_FULLSHAPE = 0x8;

    ''private const int IME_CHOTKEY_SHAPE_TOGGLE = 0x11;
    Private Const IME_CMODE_FULLSHAPE As Integer = &H8
    Private Const IME_CHOTKEY_SHAPE_TOGGLE As Integer = &H11

    'Protected Overridable Sub OnActivated(ByVal e As EventArgs)
    '    Try
    '        MyBase.OnActivated(e)

    '        Dim HIme As IntPtr = Form_ImeMode.ImmGetContext(MyBase.Handle)
    '        ''輸入法打開狀態

    '        If Form_ImeMode.ImmGetOpenStatus(HIme) Then
    '            Dim iMode As Integer = 0
    '            Dim iSentence As Integer = 0
    '            If (Form_ImeMode.ImmGetConversionStatus(HIme, iMode, iSentence) AndAlso ((iMode And 8) > 0)) Then
    '                Form_ImeMode.ImmSimulateHotKey(MyBase.Handle, &H11)
    '            End If
    '        End If

    '    Catch ex As Exception
    '        MessageBox.Show(ex.Message)
    '    End Try
    'End Sub

    Protected Overrides Sub OnImeModeChanged(ByVal e As System.EventArgs)
        MyBase.OnImeModeChanged(e)
        Dim HIme As IntPtr = Form_ImeMode.ImmGetContext(MyBase.Handle)
        ''輸入法打開狀態

        If Form_ImeMode.ImmGetOpenStatus(HIme) Then
            Dim iMode As Integer = 0
            Dim iSentence As Integer = 0
            If (Form_ImeMode.ImmGetConversionStatus(HIme, iMode, iSentence) AndAlso ((iMode And 8) > 0)) Then
                Form_ImeMode.ImmSimulateHotKey(MyBase.Handle, &H11)
            End If
        End If
    End Sub

    Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
        MyBase.OnValidating(e)
    End Sub

    Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
        MyBase.OnEnter(e)
    End Sub

    Protected Overrides Sub OnValidated(ByVal e As System.EventArgs)
        MyBase.OnValidated(e)
    End Sub

    '    protected override void OnActivated(EventArgs e)
    '{

    '    base.OnActivated(e);

    '    IntPtr HIme = ImmGetContext(this.Handle);

    '    //如果输入法处于打开状态  

    '    if (ImmGetOpenStatus(HIme))
    '    {

    '        int iMode = 0;

    '        int iSentence = 0;

    '        //检索输入法信息   

    '        bool bSuccess = ImmGetConversionStatus(HIme, ref   iMode, ref   iSentence);

    '        if (bSuccess)
    '        {

    '            //如果是全角,转换成半角  

    '            if ((iMode & IME_CMODE_FULLSHAPE) > 0)
    '            {
    '                ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE);
    '                this.Refresh();
    '                this.Update();
    '            }


    '        }
    '        //ImmSimulateHotKey(this.Handle, IME_CHOTKEY_SHAPE_TOGGLE);
    '    }

    '}
End Class